/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.business.calcs;

import TimeRecording.business.DateRange;
import TimeRecording.enums.PartOfDay;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author david
 */
public class DailyAllocation {

    private final static String PACKING = "                               ";

    private final static SimpleDateFormat DATE_FORMATTER =
            new SimpleDateFormat("dd-MMM-yyyy  ");

    private final static NumberFormat NUMBER_FORMATTER =
            new DecimalFormat("##0.00");

    private Date date;
    private double clocked;
    private double estimated;

    private boolean fixedPattern;
    private boolean semiFlexiPattern;
    private boolean flexiPattern;
    private boolean sessionalPattern;
    private boolean adHocPattern;

    private PatternAllocation fixed = new PatternAllocation();
    private PatternAllocation semiFlexi = new PatternAllocation();
    private PatternAllocation flexi = new PatternAllocation();
    private PatternAllocation sessional = new PatternAllocation();
    private PatternAllocation adHoc = new PatternAllocation();

    private HolidayAllocation holiday = new HolidayAllocation();
    private HolidayAllocation unapprovedHoliday = new HolidayAllocation();

    private SicknessAllocation sickness = new SicknessAllocation();
    private SicknessAllocation unapprovedSickness = new SicknessAllocation();

    //==========================================================================

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public double getClocked() {
        return clocked;
    }

    public void setClocked(double clocked) {
        this.clocked = clocked;
    }

    public void adjustClocked(double clocked) {
        this.clocked += clocked;
    }

    //==========================================================================

    public double getEstimated() {
        return estimated;
    }

    public void setEstimated(double estimated) {
        this.estimated = estimated;
    }

    public void adjustEstimated(double estimated) {
        this.estimated += estimated;
    }

    public void recalculateEstimated(DateRange currentPeriod) {
        if (clocked == 0.0 && currentPeriod.contains(date)) {

            estimated = fixed.getExpected() +
                    semiFlexi.getExpected() +
                    flexi.getExpected();
            
            PartOfDay absence = PartOfDay.add(
                    holiday.getPartOfDay(),
                    sickness.getPartOfDay());
            
            if (absence == PartOfDay.ALL_DAY) {
                estimated = 0.0;
                
            } else if (absence == PartOfDay.AM) {
                estimated *= 0.5;
                
            } else if (absence == PartOfDay.PM) {
                estimated *= 0.5;
            }

        } else {
            estimated = 0.0;
        }
    }

    //==========================================================================

    public boolean hasFixedPattern() {
        return fixedPattern;
    }

    public void setFixedPattern(boolean fixedPattern) {
        this.fixedPattern = fixedPattern;
    }

    public boolean hasFlexiPattern() {
        return flexiPattern;
    }

    public void setFlexiPattern(boolean flexiPattern) {
        this.flexiPattern = flexiPattern;
    }

    public boolean hasSemiFlexiPattern() {
        return semiFlexiPattern;
    }

    public void setSemiFlexiPattern(boolean semiFlexiPattern) {
        this.semiFlexiPattern = semiFlexiPattern;
    }

    public boolean hasSessionalPattern() {
        return sessionalPattern;
    }

    public void setSessionalPattern(boolean sessionalPattern) {
        this.sessionalPattern = sessionalPattern;
    }

    public boolean hasAdHocPattern() {
        return adHocPattern;
    }

    public void setAdHocPattern(boolean adHocPattern) {
        this.adHocPattern = adHocPattern;
    }

    //==========================================================================

    public PatternAllocation getFixed() {
        return fixed;
    }

    public PatternAllocation getSemiFlexi() {
        return semiFlexi;
    }

    public PatternAllocation getFlexi() {
        return flexi;
    }

    public PatternAllocation getSessional() {
        return sessional;
    }

    public PatternAllocation getAdHoc() {
        return adHoc;
    }

    //==========================================================================

    public HolidayAllocation getHoliday() {
        return holiday;
    }

    public HolidayAllocation getUnapprovedHoliday() {
        return unapprovedHoliday;
    }

    //==========================================================================

    public SicknessAllocation getSickness() {
        return sickness;
    }

    public SicknessAllocation getUnapprovedSickness() {
        return unapprovedSickness;
    }

    //==========================================================================

    public double getTotalPatternTime() {
        return fixed.getExpected() +
                semiFlexi.getExpected() +
                flexi.getExpected() +
                sessional.getExpected();
    }

    public double getTotalClockedTime() {
        return fixed.getWorked() +
                semiFlexi.getWorked() +
                flexi.getWorked() +
                sessional.getWorked() +
                adHoc.getWorked();
    }

    public double getTotalHolidayTime() {
        return fixed.getHoliday() +
                semiFlexi.getHoliday() +
                flexi.getHoliday() +
                sessional.getHoliday() +
                adHoc.getHoliday();
    }

    public double getTotalSicknessTime() {
        return fixed.getSickness() +
                semiFlexi.getSickness() +
                flexi.getSickness() +
                sessional.getSickness() +
                adHoc.getSickness();
    }

    //==========================================================================

    @Override
    public String toString() {
        
        return date + ", " +
                "estimated=" + estimated + ", " +
                "clocked=" + clocked + ", " +
                "fixed=[" + fixed + "], " +
                "semiFlexi=[" + semiFlexi + "], " +
                "flexi=[" + semiFlexi + "], " +
                "holiday=[" + holiday + "], " +
                "unapprovedHoliday=[" + unapprovedHoliday + "], " +
                "sickness=[" + sickness + "], " +
                "unapprovedSickness=[" + unapprovedSickness + "]";
    }

    private static String pad(String string, int length) {
        if (string.length() < length) {
            return string + PACKING.substring(0, length - string.length());
        } else {
            return string;
        }
    }
}
